package com.zhejiangzhengyuan.municipal_flooded_road.controller;

import com.zhejiangzhengyuan.municipal_common.service.*;
import com.zhejiangzhengyuan.municipal_common.utils.LED.URLUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import water_ruler_threads.ServerThread;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author ZhouXiang
 * @since on 2020/1/3 接收道路积水物联网数据
 */
@Component
@Slf4j
@Api(tags = "接收道路积水物联网数据")
public class InitApplication implements ApplicationRunner {
  private static ExecutorService exe1 = Executors.newFixedThreadPool(10);
  private static final Integer port = 11111;

  @Autowired private DeviceService deviceService;
  @Autowired private DeviceMonitorValueService deviceMonitorValueService;
  @Autowired private DeviceDataService deviceDataService;
  @Autowired private WarnEventService warnEventService;
  @Autowired private ElectronicDataService electronicDataService;
  @Autowired private LedBindingService ledBindingService;
  @Autowired private LedHistoryService ledHistoryService;
  @Autowired private URLUtils urlUtils;

  @Override
  public void run(ApplicationArguments args) throws Exception {
    ServerSocket serverSocket = new ServerSocket(port);
    log.info("服务已启动");
    while (true) {
      Socket socket = serverSocket.accept();
      log.info("客户:{} ,来访，端口为：{}", socket.getInetAddress().getHostAddress(), socket.getPort());
      exe1.execute(
          new ServerThread(
              socket,
              deviceService,
              deviceMonitorValueService,
              deviceDataService,
              warnEventService,
              electronicDataService,
              ledBindingService,
              ledHistoryService,
              urlUtils));
    }
  }
}
